<template>
  <div class="orderpay-container nav-bar-top">
    <!-- 导航栏 -->
    <van-nav-bar class="nav-bar" title="订单支付" left-arrow @click-left="$router.replace('/user')"/>
    <!-- 倒计时 -->
    <div class="countdown">
      <van-count-down @change="timeChange" :time="time" format="剩余支付时间 HH:mm:ss"/>
      <span class="money">¥{{orderInfo.orderPayMoney | fentoyuan}}</span>
    </div>
    <!-- 支付方式 -->
    <van-radio-group class="circle-bg" v-model="payType">
      <van-cell title="余额支付" @click="payType = '0'">
        <div slot="icon" class="pay-icon">
          <van-image class="pay-img" width="23" height="23" src="imgs/user/order/wallet_pay.png" />
        </div>
        <div slot="extra" class="pay-radio">
          <van-radio name="0"></van-radio>
        </div>
      </van-cell>
      <van-cell title="微信支付" @click="payType = '1'">
        <div slot="icon" class="pay-icon">
          <van-image class="pay-img" width="23" height="23" src="imgs/user/order/wx_pay.png" />
        </div>
        <div slot="extra">
          <van-radio name="1"></van-radio>
        </div>
      </van-cell>
      <van-cell title="支付宝支付" @click="payType = '2'">
        <div slot="icon" class="pay-icon">
          <van-image class="pay-img" width="23" height="23" src="imgs/user/order/ali_pay.png" />
        </div>
        <div slot="extra" class="pay-radio">
          <van-radio name="2"></van-radio>
        </div>
      </van-cell>
    </van-radio-group>
    <!-- 支付按钮 -->
    <div class="pay-btn">
      <van-button :loading="submitLoading" :disabled="time <= 0 || !canPay" @click="orderPay" block>确认支付 ¥{{orderInfo.orderPayMoney | fentoyuan}}</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import constants from '@/config/constants'
import orderApi from '@/api/orderApi';

export default {
  name: 'orderPay',
  data () {
    return {
      /* 支付方式：0余额 1微信 2支付宝 */
      payType: '0',
      /* 支付倒计时 */
      canPay: true,
      time: 15*60*1000,
      submitLoading: false,
      /* 订单信息 */
      orderInfo: {}
    }
  },
  computed: {
    ...mapState(['orderSetting'])
  },
  created () {
    this.getOrderDetail();
  },
  methods: {
    /* 获取订单详情 */
    async getOrderDetail () {
      const result = await orderApi.orderDetail(this.$route.query.id);
      if(result.code == constants.SUCCESS){
        this.orderInfo = result.data;
        var overTime = new Date().getTime() - new Date(result.data.createTime).getTime();
        this.time = parseInt(this.orderSetting.payTimeout)*60*1000 - overTime - 1*60*1000;
      }
    },
    /* 支付 */
    async orderPay () {
      if(this.payType == '0'){
        this.$toast.loading('支付中');
        this.submitLoading = true;
        const result = await orderApi.orderPay4Balance(this.orderInfo.id);
        if(result.code == constants.SUCCESS){
          this.$store.commit(constants.STORE.UPDATE_CART_LIST);
          this.$toast.clear();
          this.$router.replace({path:'/orderPayResult', query: {id: this.orderInfo.id}});
          this.submitLoading = false;
        }
      }else{
        this.$toast.fail("暂不支持该支付方式!");
      }
    },
    /* 倒计时变化 */
    timeChange (timeData) {
      if(timeData.seconds <= 0 && timeData.minutes <=0){
        this.canPay = false;
      }
    }
  }
}
</script>

<style lang="scss">
.orderpay-container{
  padding-top: 5px;
  .countdown{
    height: 120px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .van-count-down{
      font-size: 14px;
      color: #c4c4c4;
    }
    .money{
      color: #e1212b;
      font-size: 30px;
      margin-top: 15px;
    }
  }
  .pay-icon{
    display: flex;
    align-items: center;
    .pay-img{
      margin-right: 10px;
    }
  }
  .pay-radio {
    display: flex;
    align-items: center;
    .van-radio{
      display: flex;
      align-items: center;
    }
  }
  .van-radio-group {
    .van-cell{
      padding: 15px 15px;
    }
    .van-radio{
      margin-bottom: 0px;
    }
  }
  .pay-btn{
    margin-top: 15px;
    padding: 0px 10px;
    display: flex;
    justify-content: center;
  }
}
</style>
